import 'dart:io' as io;
import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:ui_demo/local_storage/sqflite/product_bean.dart';

import 'list_page.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // Init ffi loader if needed.
  sqfliteFfiInit();

  var databaseFactory = databaseFactoryFfi;
  final io.Directory appDocumentsDir = await getApplicationDocumentsDirectory();
  // final io.Directory? appExternalStorage = await getExternalStorageDirectory();
  print("appDocumentsDir:${appDocumentsDir.path}");
  // print("appExternalStorage:${appExternalStorage?.path}");
  //Create path for database
  String dbPath = p.join(appDocumentsDir.path, "databases", "myDb.db");
  var db = await databaseFactory.openDatabase(
    dbPath,
  );

  // var result = await db.query('Product');
  // print(result);

  // if (result.isEmpty) {
  await db.execute('''
  CREATE TABLE IF NOT exists Product (
      id INTEGER PRIMARY KEY,
      title TEXT,
      content TEXT,
      date TEXT
  )
  ''');
  // }
  var productBean =
      ProductBean("dart教材", "dart教材dart教材dart教材dart教材dart教材", "2024-2-19");

  print(productBean.toJson());
  await db
      .insert('Product', productBean.toJson())
      .then((value) => print(value));
  // await db.insert('Product', <String, Object?>{'title': 'Product 1'});
  // await db.insert('Product', <String, Object?>{'title': 'Product 2'});

  var result = await db.query('Product');
  for (var element in result) {
    var productBean = ProductBean.fromJson(element);
    print(productBean.title);
  }
  print(result);

  var productBean2 = ProductBean("flutter", "flutter教材", "2024-2-19");
  await db.update('Product', productBean2.toJson(),
      where: 'title = ?', whereArgs: ["dart教材"]).then((value) => print(value));
  // prints [{id: 1, title: Product 1}, {id: 2, title: Product 1}]

  var result1 = await db.query('Product');
  for (var element in result1) {
    var productBean = ProductBean.fromJson(element);
    print(productBean.title);
  }
  print(result1);

  var result2 = await db.delete('Product');
  print(result2);


  await db.close();

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'NotePad',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with 'flutter run'. You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // 'hot reload' (press 'r' in the console where you ran 'flutter run',
        // or simply save your changes to 'hot reload' in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const NoteListPage(),
    );
  }
}
